package com.util;

import com.android.ninepatch.NinePatch;
import net.coobird.thumbnailator.Thumbnails;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

public class DisposeNinePatch {
    public static File creatLargePic(String src,int width,int height){
        File ninePatch = new File(src);
        String temp = ninePatch.getParent().substring(0,ninePatch.getParent().lastIndexOf("\\"));
        File largePic = new File(temp.substring(0,temp.lastIndexOf("\\"))+"/temp");
        if(!largePic.exists()){
            largePic.mkdirs();
        }
        File file = new File(largePic.getAbsolutePath()+ "/"+ ninePatch.getName());
        BufferedImage bi = null;
        try {
            bi = ImageIO.read(new FileInputStream(ninePatch));
            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D g1 = image.createGraphics();
            image = g1.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
            g1.dispose();
            g1 = image.createGraphics();

            NinePatch np = NinePatch.load(bi,true,false);
            np.draw(g1,0,0,width,height);
            ImageIO.write(image,"png",file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }

    public static InputStream zoomPic(File file, int width, int height){
        InputStream is = null;
        try {
            Thumbnails.of(file.getAbsolutePath()).size(480, 50).keepAspectRatio(false).toFile(
                    file.getParent() + "/s" + file.getName());
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            is = new FileInputStream(file.getParent() + "/s" + file.getName());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        return is;
    }

}
